package net.wenku8.reader.api

import net.wenku8.reader.entity.ApiResult
import net.wenku8.reader.model.user.SignDataList
import net.wenku8.reader.model.user.UserDataList
import net.wenku8.reader.model.user.UserInfo
import net.wenku8.reader.model.user.UserSignInfo
import net.wenku8.reader.service.SessionKeepService
import net.wenku8.reader.state.StateLogin
import net.wenku8.reader.util.TextType
import net.wenku8.reader.util.ValidateRule
import net.wenku8.reader.util.checkRule
import net.wenku8.reader.util.parseXml


class UserApi{

    companion object {
        fun login(username: String, password: String): ApiResult<String> {
            val ruleList = listOf(
                ValidateRule(username, "用户名", required = true, min = 3),
                ValidateRule(password, "密码", required = true, TextType.ANS, min = 6),
            )
            val msg = checkRule(ruleList)
            if (!msg.isNullOrBlank()) {
                return ApiResult.fail(msg)
            }
            val map = mapOf(
                "action" to if (username.contains("@")) "loginemail" else "login",
                "username" to username,
                "password" to password,
            )
            val apiResult = Wenku8Connect.request(map)
            if (ApiResult.isSuccess(apiResult)){
                StateLogin.username.value = username
                StateLogin.password.value = password
                StateLogin.status.value = true
                StateLogin.sessionId.value = apiResult.data!!
                SessionKeepService.start()
            }
            return apiResult
        }

        fun logout(): ApiResult<String> {
            val map = mapOf(
                "action" to "logout",
            )
            val apiResult = Wenku8Connect.request(map)
            if (ApiResult.isSuccess(apiResult)){
                StateLogin.status.value = false
                StateLogin.sessionId.value = ""
                SessionKeepService.stop()
            }
            return apiResult
        }

        fun getAvatar(): ApiResult<String> {
            val map = mapOf(
                "action" to "avatar",
            )
            return Wenku8Connect.request(map)
        }

        fun getUserInfo(): ApiResult<UserInfo> {
            val map = mapOf(
                "action" to "userinfo",
            )
            val apiResult = Wenku8Connect.request(map)
            if (!ApiResult.isSuccess(apiResult)){
                return ApiResult.fail(apiResult.message)
            }
            val userDataList = parseXml(apiResult.data, UserDataList::class.java)
            val userInfo = itemAsUserInfo(userDataList)
            return ApiResult.success(userInfo)
        }

        fun doUserSign(): ApiResult<UserSignInfo> {
            val map = mapOf(
                "action" to "block",
                "do" to "sign",
            )
            val apiResp = Wenku8Connect.request(map)
            if (!ApiResult.isSuccess(apiResp)){
                return ApiResult.fail(apiResp.message)
            }
            val signDataList = parseXml(apiResp.data, SignDataList::class.java)
            val userSignInfo = itemAsUserSignInfo(signDataList)
            return ApiResult.success(userSignInfo)
        }


    }



}